package com.fc.v2.common.port;

import java.math.BigInteger;

/**
 * @program:
 * @description: 十六进制转化工具
 * @author:
 * @create: 2024-10-13 12:07
 **/
public class HexUtils {
    private static final char[] HEXES = {
            '0', '1', '2', '3',
            '4', '5', '6', '7',
            '8', '9', 'a', 'b',
            'c', 'd', 'e', 'f'
    };

    /**
     * bytes[]转换成16进制字符串
     *
     * @param bytes
     * @return
     */
    public static String byteToHexStr(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        StringBuilder hex = new StringBuilder();
        for (byte b : bytes) {
            hex.append(HEXES[(b >> 4) & 0x0F]);
            hex.append(HEXES[b & 0x0F]);
        }
        return hex.toString().toUpperCase();
    }

    /**
     * 16进制字符串转 byte[]
     *
     * @param hex
     * @return
     */
    public static byte[] hexStrToBytes(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }
        char[] hexChars = hex.toCharArray();
        byte[] bytes = new byte[hexChars.length / 2];   // 如果 hex 中的字符不是偶数个, 则忽略最后一个
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
        }
        return bytes;
    }

    /**
     * 16进制字符串转10进制数字
     *
     * @param hex 16进制字符串
     * @return
     */
    public static int hexStrToInt(String hex) {
        BigInteger bigint = new BigInteger(hex, 16);
        return bigint.intValue();
    }

    /**
     * 16进制字符串转字符串
     *
     * @param hexStr 16进制字符串
     * @return
     */
    public static String hexStrToStr(String hexStr) {
        //能被16整除,肯定可以被2整除
        byte[] array = new byte[hexStr.length() / 2];
        try {
            for (int i = 0; i < array.length; i++) {
                array[i] = (byte) (0xff & Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16));
            }
            hexStr = new String(array, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return hexStr;
    }

    /**
     * 字符串转换成16进制字符串
     *
     * @param str 待转换的ASCII字符串
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     */
    public static String strToHexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            //sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * int 转换为对应的 16进制字符串
     */
    public static String intHex2(Integer hex) {
        if (hex == null) {
            return null;
        }
        String s1= Integer.toHexString(hex).toUpperCase();
        return addZeroForNum(s1, 2);
    }

    /**
     * String前面补0
     */
    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        while (strLen < strLength) {
            StringBuffer sb = new StringBuffer();
            sb.append("0").append(str);// 左补0
			//sb.append(str).append("0");// 右补0
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }
}
